//import 'package:amap_base/amap_base.dart';

import 'dart:async';
import 'dart:convert';

import 'package:date_format/date_format.dart';
import 'package:dio/dio.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_datetime_picker/flutter_datetime_picker.dart';
import 'package:flutter_hotwork/HomePage.dart';
import 'package:flutter_hotwork/Myapplication.dart';
import 'package:flutter_hotwork/bean/BaseBean.dart';
import 'package:flutter_hotwork/home/RoadChoice.dart';
import 'package:flutter_hotwork/homepage/BaseTitleBar.dart';
import 'package:flutter_hotwork/homepage/BaseViewBar.dart';
import 'package:flutter_hotwork/utils/DioUtils.dart';
import 'package:fluttertoast/fluttertoast.dart';
// import 'package:flutter_map/flutter_map.dart';
import 'package:latlong/latlong.dart';

import '../Login.dart';
import '../Toast.dart';
import 'YongHuYongLiangShenBao.dart';

class shenbaoactivity extends StatefulWidget {
  @override
  baseState createState() => new baseState();
}

class baseState extends State<shenbaoactivity> {

  @override
  void initState() {
    super.initState();
    DioUtils.createInstance();
  }
//手机号的控制器
  TextEditingController phoneController = TextEditingController();
  @override
  void dispose() {
//    _amapLocation.stopLocate();
    super.dispose();
  }
  Map selectArea;
  var time = formatDate(new DateTime.now(),[yyyy,'-',mm,"-",dd]);
  var time2 = formatDate(new DateTime.now(),[yyyy,'-',mm,"-",dd]);
  TabController _tabController;
  int sex = 1;
  bool check = false;
  ///接收选择的结果
  void handleSelect (Map targetArea) {
    setState(() {
      selectArea = targetArea;
      // initData(selectArea['proviceIndex'],selectArea['cityIndex']);
    });
  }
  void submit() async {
    try {
//      Response response = await DioUtils.dio.get("http://www.baidu.com");
      var reqeuest =Map<Object,Object>();
      reqeuest["steamAmount"] = phoneController.text;
      reqeuest["meterId"] = selectArea['id'];
      reqeuest["startTime"] = time;
      reqeuest["endTime"] = time2;


      try {
        Response response;
        response = await DioUtils.dio.post(DioUtils.API_PREFIX+"/declareSteam/addDeclare", data: json.encode(reqeuest));

        Map respMap = json.decode(response.data);
        basebean base = basebean.fromJson(respMap);
        if (base.code == 200) {
           LocalToast.toast(context,msg: "提交成功");
           Navigator.of(context).pop();
        } else if(base.code == 500){
          LocalToast.toast(context,msg:"会话超时，请重新登陆！");
          Navigator.pushAndRemoveUntil(
            context,
            new MaterialPageRoute(
                builder: (context) => new MyHomePage()),
                (route) => route == null,
          );
        } else {
           LocalToast.toast(context,msg: base.message);
        }
        print('响应数据：' + response.toString());
      } catch (e) {
        //  LocalToast.toast(context,msg: "网络错误");
        print('请求出错：' + e.toString());
      }



      print(phoneController.text);
//      print(response);
    } catch (e) {
      print(e);
    }
  }
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar:new BaseViewBar(
          childView:
          Column(
            children: [
              new BaseTitleBar(
                "用户用量申报",
                leftIcon: 'images/fanhui.png',
                rightIcon:  "",
                rightClick: () {
                  print("点击了右边干嘛啊。。。哦");
                },
                leftClick: () {
                  Navigator.of(context).pop();
                },
              )
              ,
            ],
          ),
          preferredSize: Size.fromHeight(58.0)

      ),
      body:
          Container(
          padding: EdgeInsets.only(left: 10),
          child:
          Column(
            children: [
              Row(
                children: <Widget>[
                  Text("用户名称："),
                  Container(
                    width: 10,
                  ),
                  Container(
                    margin:  EdgeInsets.only(left: 5, right: 0, top:5, bottom: 5),
                    child:
                    InkWell(
                      onTap: (){
                        showModalBottomSheet(context: context,builder: (_)=>Container(
                          height: 200.0,
                          child: AreaSelection(
                            onSelect: handleSelect,
                            initProviceIndex: selectArea == null ? 0 : selectArea['proviceIndex'],
                            initCityIndex: selectArea == null ? 0 : selectArea['cityIndex'],
                          ),
                        ));
                      },
                      child:
                      Container(
                          height: 20,
                          margin: EdgeInsets.only(left: 10, right: 0, top:0, bottom: 0),
                          padding:  EdgeInsets.only(left: 2, right: 0, top:0, bottom: 0),
                          decoration: BoxDecoration(
                            border: new Border.all(color: Colors.blue, width: 0.5), // 边色与边宽度
                            color: Colors.white, // 底色
                            //        borderRadius: new BorderRadius.circular((20.0)), // 圆角度
                          ),
                          child:
                          Row(
                            children: [
                              Container(
                                width:100,
                                child:
                                Text(selectArea != null ? selectArea['address'] : "请选择用户"  ,
                                  style: TextStyle(color: Colors.black,fontSize: 10),
                                  overflow: TextOverflow.ellipsis,
                                ) ,
                              ),

                              Container(width: 5,),
                              Image.asset(
                                'images/jiantou.png',
                                width: 14.0,
                                height: 20.0,
                              ),
                            ],
                          )
                      ),
                    ),
                  ),
                ],
              ),
              Container(height: 10,),
              Row(
                children: <Widget>[
                  Text("开始时间："),
                  Container(
                    width: 10,
                  ),
                  FlatButton(
                    onPressed: () {
                      DatePicker.showDatePicker(context,
                          // 是否展示顶部操作按钮
                          showTitleActions: true,
                          // 最小时间
                          minTime: DateTime(2018, 3, 5),
                          // 最大时间
                          maxTime: DateTime(2099, 6, 7),
                          // change事件
                          onChanged: (date) {


                            print('change $date');
                          },
                          // 确定事件
                          onConfirm: (date) {
                            time = formatDate(date,[yyyy,'-',mm,"-",dd]);
                            setState(() {

                            });
                            print('confirm $time');
                          },
                          // 当前时间
                          currentTime: DateTime.now(),
                          // 语言
                          locale: LocaleType.zh);
                    },
                    // onPressed: () {
                    //   DatePicker.showDateTimePicker(context,
                    //       // 是否展示顶部操作按钮
                    //       showTitleActions: true,
                    //       // 最小时间
                    //       minTime: DateTime(2018, 3, 5),
                    //       // 最大时间
                    //       maxTime: DateTime(2099, 6, 7),
                    //       // change事件
                    //       onChanged: (date) {
                    //
                    //
                    //         print('change $date');
                    //       },
                    //       // 确定事件
                    //       onConfirm: (date) {
                    //         time = formatDate(date,[yyyy,'-',mm,"-",dd,"  ",HH,":",nn]);
                    //         setState(() {
                    //
                    //         });
                    //         print('confirm $time');
                    //       },
                    //       // 当前时间
                    //       currentTime: DateTime.now(),
                    //       // 语言
                    //       locale: LocaleType.zh);
                    // },
                    child:
                    Container(
                        width: 100,
                        height: 20,
                        margin: EdgeInsets.only(left: 0, right: 0, top:0, bottom: 0),
                        padding:  EdgeInsets.only(left: 10, right: 0, top:0, bottom: 0),
                        decoration: BoxDecoration(
                          border: new Border.all(color: Colors.blue, width: 0.5), // 边色与边宽度
                          color: Colors.white, // 底色
                          //        borderRadius: new BorderRadius.circular((20.0)), // 圆角度
                        ),
                        child:
                        Row(
                          children: [
                            Image.asset(
                              'images/rili.png',
                              width: 14.0,
                              height: 20.0,
                            ),
                            Container(width: 5,),
                            Text(
                              '$time',
                              style: TextStyle(color: Colors.black,fontSize: 10),
                            )

                          ],
                        )
                    ),
                  ),
                ],
              ),
              Row(
                children: <Widget>[
                  Text("结束时间："),
                  Container(
                    width: 10,
                  ),
                  FlatButton(
                    onPressed: () {
                      DatePicker.showDatePicker(context,
                          // 是否展示顶部操作按钮
                          showTitleActions: true,
                          // 最小时间
                          minTime: DateTime(2018, 3, 5),
                          // 最大时间
                          maxTime: DateTime(2099, 6, 7),
                          // change事件
                          onChanged: (date) {


                            print('change $date');
                          },
                          // 确定事件
                          onConfirm: (date) {
                            time2 = formatDate(date,[yyyy,'-',mm,"-",dd]);
                            setState(() {

                            });
                          },
                          // 当前时间
                          currentTime: DateTime.now(),
                          // 语言
                          locale: LocaleType.zh);
                    },
                    child:
                    Container(
                        width: 100,
                        height: 20,
                        margin: EdgeInsets.only(left: 0, right: 0, top:0, bottom: 0),
                        padding:  EdgeInsets.only(left: 10, right: 0, top:0, bottom: 0),
                        decoration: BoxDecoration(
                          border: new Border.all(color: Colors.blue, width: 0.5), // 边色与边宽度
                          color: Colors.white, // 底色
                          //        borderRadius: new BorderRadius.circular((20.0)), // 圆角度
                        ),
                        child:
                        Row(
                          children: [
                            Image.asset(
                              'images/rili.png',
                              width: 14.0,
                              height: 20.0,
                            ),
                            Container(width: 5,),
                            Text(
                              '$time2',
                              style: TextStyle(color: Colors.black,fontSize: 10),
                            )

                          ],
                        )
                    ),
                  ),
                ],
              ),

              Row(
                children: <Widget>[
                  Text("用汽量(t)："),
                  Container(
                    width: 10,
                  ),
                  Container(
                    width: 100,
                    height: 20,
                    margin: EdgeInsets.only(left: 15),
                    padding:  EdgeInsets.only(left: 10, right: 0, top:0, bottom: 0),
                    decoration: BoxDecoration(
                      border: new Border.all(color: Color(0xFF9E9E9E), width: 0.5), // 边色与边宽度
                      color: Colors.white, // 底色
                      //        borderRadius: new BorderRadius.circular((20.0)), // 圆角度
                      borderRadius: BorderRadius.all(Radius.circular(15)),
                    ),
                    child:
                    TextField(
                      inputFormatters: [WhitelistingTextInputFormatter(RegExp("[0-9.]")),],//只允许输入数字
                      style: TextStyle(color: Colors.grey),
                      controller: phoneController,
                      // controller: phoneController,
                      keyboardType: TextInputType.number,
                      decoration: InputDecoration(
                        contentPadding: EdgeInsets.all(10.0),

                        hintStyle: TextStyle(color: Colors.grey),
//                            helperText: '请输入注册的手机号',
                        border:InputBorder.none,
                      ),
                      autofocus: false,
                    ),

                  ),
                  Container(
                    width: 10,
                  ),
                ],
              ),
              Container(height: 50,),
              Row(
                mainAxisAlignment:MainAxisAlignment.center,
                children: [
                  InkWell(
                    onTap:
                        (){
                      submit();
                    }

                    ,
                    child:
                  Container(
                    width: 200,
                    height: 30,
                    decoration: BoxDecoration(
                      color: Colors.blue, // 底色
                      //        borderRadius: new BorderRadius.circular((20.0)), // 圆角度
                      borderRadius: BorderRadius.all(Radius.circular(15)),
                    ),
                    child:

                        Center(
                          child:
                         Text("提交",style: TextStyle(color: Colors.white),),
                          )


                        ),

                  ),
                ],
              ),

            ],
          )


        ),
    );
  }


}






















